/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.server.visitor.impl;

import com.genia.toolbox.web.gwt.form.client.form.BasicForm;
import com.genia.toolbox.web.gwt.form.client.form.ButtonItem;
import com.genia.toolbox.web.gwt.form.client.form.CheckBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.DateItem;
import com.genia.toolbox.web.gwt.form.client.form.DateTimeItem;
import com.genia.toolbox.web.gwt.form.client.form.DynamicListBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.DynamicRadioButtonItem;
import com.genia.toolbox.web.gwt.form.client.form.EnumDescriptableValuedItemEntryList;
import com.genia.toolbox.web.gwt.form.client.form.FileUploadItem;
import com.genia.toolbox.web.gwt.form.client.form.Form;
import com.genia.toolbox.web.gwt.form.client.form.HorizontalForm;
import com.genia.toolbox.web.gwt.form.client.form.I18nTextAreaItem;
import com.genia.toolbox.web.gwt.form.client.form.I18nTextBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.ImageItem;
import com.genia.toolbox.web.gwt.form.client.form.InlineFormMultipleItem;
import com.genia.toolbox.web.gwt.form.client.form.InlineFormSimpleItem;
import com.genia.toolbox.web.gwt.form.client.form.LabelItem;
import com.genia.toolbox.web.gwt.form.client.form.LabeledForm;
import com.genia.toolbox.web.gwt.form.client.form.ListBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.MultipleFileUploadItem;
import com.genia.toolbox.web.gwt.form.client.form.PasswordBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.RadioButtonItem;
import com.genia.toolbox.web.gwt.form.client.form.RichTextAreaItem;
import com.genia.toolbox.web.gwt.form.client.form.SimpleItem;
import com.genia.toolbox.web.gwt.form.client.form.SimpleValuedItemEntryList;
import com.genia.toolbox.web.gwt.form.client.form.SubFormMultipleItem;
import com.genia.toolbox.web.gwt.form.client.form.SubFormSimpleItem;
import com.genia.toolbox.web.gwt.form.client.form.TabForm;
import com.genia.toolbox.web.gwt.form.client.form.TextAreaItem;
import com.genia.toolbox.web.gwt.form.client.form.TextBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.TimeItem;
import com.genia.toolbox.web.gwt.form.client.form.VerticalForm;
import com.genia.toolbox.web.gwt.form.client.value.FormValues;
import com.genia.toolbox.web.gwt.form.client.value.impl.FormSimpleValueImpl;
import com.genia.toolbox.web.gwt.form.client.visitor.FormVisitor;

/**
 * Initialize form values.
 */
public class InitializeValuesFormVisitor
    implements FormVisitor
{

  /**
   * Contains values to initialize.
   */
  private FormValues values;



  /**
   * Constructor with values to initialize.
   * 
   * @param values
   *          to initialize
   */
  public InitializeValuesFormVisitor(FormValues values)
  {
    this.values = values;
  }



  /**
   * Initialize item.
   * 
   * @param simpleItem
   *          to initialize
   */
  private void visitSimpleItem(SimpleItem simpleItem)
  {
    visitSimpleItem(simpleItem, null);
  }



  /**
   * Initialize item.
   * 
   * @param simpleItem
   *          the item to initialize the value for
   * @param value
   *          the initial value
   */
  private void visitSimpleItem(SimpleItem simpleItem, String value)
  {

    FormSimpleValueImpl fv = new FormSimpleValueImpl();
    fv.setValue(value);
    fv.setName(simpleItem.getName());
    values.addFormSimpleValue(fv);

  }



  /**
   * Initialize basic forms.
   * 
   * @param form
   *          to initialize
   */
  private void visitBasicForm(BasicForm<? extends Form> form)
  {
    for (Form subForm : form.getForms()) {
      subForm.accept(this);
    }
  }



  /**
   * Initialize checkBoxItem.
   * 
   * @param checkBoxItem
   *          to initialize
   */
  public void visitCheckBoxItem(final CheckBoxItem checkBoxItem)
  {
    visitSimpleItem(checkBoxItem);
  }



  /**
   * Initialize horizontalForm.
   * 
   * @param horizontalForm
   *          to initialize
   */

  public void visitHorizontalForm(final HorizontalForm horizontalForm)
  {

    visitBasicForm(horizontalForm);

  }



  /**
   * Initialize imageItem.
   * 
   * @param imageItem
   *          to initialize
   */
  public void visitImageItem(final ImageItem imageItem)
  {

    visitSimpleItem(imageItem);

  }



  /**
   * Initialize labelItem.
   * 
   * @param labelItem
   *          to initialize
   */
  public void visitLabelItem(final LabelItem labelItem)
  {
    visitSimpleItem(labelItem);
  }



  /**
   * visit a {@link ButtonItem}.
   * 
   * @param buttonItem
   *          the {@link ButtonItem} to visit
   */
  public void visitButtonItem(ButtonItem buttonItem)
  {
    visitSimpleItem(buttonItem);
  }



  /**
   * Initialize listBoxItem.
   * 
   * @param listBoxItem
   *          to initialize
   */
  public void visitListBoxItem(final ListBoxItem listBoxItem)
  {

    visitSimpleItem(listBoxItem);

  }



  /**
   * Initialize passwordBoxItem.
   * 
   * @param passwordBoxItem
   *          to initialize
   */
  public void visitPasswordBoxItem(final PasswordBoxItem passwordBoxItem)
  {

    visitSimpleItem(passwordBoxItem);

  }



  /**
   * Initialize radioButtonItem.
   * 
   * @param radioButtonItem
   *          to initialize
   */
  public void visitRadioButtonItem(final RadioButtonItem radioButtonItem)
  {

    visitSimpleItem(radioButtonItem);

  }



  /**
   * Initialize richTextAreaItem.
   * 
   * @param richTextAreaItem
   *          to initialize
   */
  public void visitRichTextAreaItem(final RichTextAreaItem richTextAreaItem)
  {

    visitSimpleItem(richTextAreaItem);

  }



  /**
   * Initialize subFormMultipleItem.
   * 
   * @param subFormMultipleItem
   *          to initialize
   */
  public void visitSubFormMultipleItem(SubFormMultipleItem subFormMultipleItem)
  {

    visitSimpleItem(subFormMultipleItem);

  }



  /**
   * Initialize subFormSimpleItem.
   * 
   * @param subFormSimpleItem
   *          to initialize
   */
  public void visitSubFormSimpleItem(final SubFormSimpleItem subFormSimpleItem)
  {
    visitSimpleItem(subFormSimpleItem);
  }



  /**
   * Initialize tabForm.
   * 
   * @param tabForm
   *          to initialize
   */
  public void visitTabForm(final TabForm tabForm)
  {
    visitBasicForm(tabForm);
  }



  /**
   * Initialize textAreaItem.
   * 
   * @param textAreaItem
   *          to initialize
   */
  public void visitTextAreaItem(final TextAreaItem textAreaItem)
  {
    visitSimpleItem(textAreaItem);
  }



  /**
   * Initialize textBoxItem.
   * 
   * @param textBoxItem
   *          to initialize
   */
  public void visitTextBoxItem(final TextBoxItem textBoxItem)
  {
    visitSimpleItem(textBoxItem);
  }



  /**
   * Initialize dateItem.
   * 
   * @param dateItem
   *          to initialize
   */
  public void visitDateItem(DateItem dateItem)
  {
    visitSimpleItem(dateItem, String.valueOf(System.currentTimeMillis()));
  }



  /**
   * Initialize timeItem.
   * 
   * @param timeItem
   *          to initialize
   */
  public void visitTimeItem(TimeItem timeItem)
  {
    visitSimpleItem(timeItem, String.valueOf(System.currentTimeMillis()));
  }



  /**
   * Initialize dateTimeItem.
   * 
   * @param dateTimeItem
   *          to initialize
   */
  public void visitDateTimeItem(DateTimeItem dateTimeItem)
  {
    visitSimpleItem(dateTimeItem, String.valueOf(System.currentTimeMillis()));
  }



  /**
   * Initialize fileUploadItem.
   * 
   * @param fileUploadItem
   *          to initialize
   */
  public void visitFileUploadItem(FileUploadItem fileUploadItem)
  {
    visitSimpleItem(fileUploadItem);
  }



  /**
   * Initialize multipleFileUploadItem.
   * 
   * @param multipleFileUploadItem
   *          to initialize
   */
  public void visitMultipleFileUploadItem(MultipleFileUploadItem multipleFileUploadItem)
  {
    visitSimpleItem(multipleFileUploadItem);
  }



  /**
   * Initialize verticalForm.
   * 
   * @param verticalForm
   *          to initialize
   */
  public void visitVerticalForm(VerticalForm verticalForm)
  {

    visitBasicForm(verticalForm);

  }



  /**
   * Initialize valuedItemEntryList.
   * 
   * @param valuedItemEntryList
   *          to initialize
   */
  public void visitEnumDescriptableValuedItemEntryList(EnumDescriptableValuedItemEntryList valuedItemEntryList)
  {

  }



  /**
   * Initialize valuedItemEntryList.
   * 
   * @param valuedItemEntryList
   *          to initialize
   */
  public void visitSimpleValuedItemEntryList(SimpleValuedItemEntryList valuedItemEntryList)
  {

  }



  /**
   * Initialize labeledForm.
   * 
   * @param labeledForm
   *          to initialize
   */
  public void visitLabeledForm(LabeledForm labeledForm)
  {

    labeledForm.getSubForm().accept(this);

  }



  /**
   * Initialize i18nTextAreaItem.
   * 
   * @param i18nTextAreaItem
   *          to initialize
   */
  public void visitI18nTextAreaItem(I18nTextAreaItem i18nTextAreaItem)
  {
    visitSimpleItem(i18nTextAreaItem);
  }



  /**
   * Initialize i18nTextBoxItem.
   * 
   * @param i18nTextBoxItem
   *          to initialize
   */
  public void visitI18nTextBoxItem(I18nTextBoxItem i18nTextBoxItem)
  {
    visitSimpleItem(i18nTextBoxItem);
  }



  /**
   * visit a {@link InlineFormMultipleItem}.
   * 
   * @param inlineFormMultipleItem
   *          the {@link InlineFormMultipleItem} to visit
   * @see com.genia.toolbox.web.gwt.form.client.visitor.FormVisitor#visitInlineFormMultipleItem(com.genia.toolbox.web.gwt.form.client.form.InlineFormMultipleItem)
   */
  public void visitInlineFormMultipleItem(InlineFormMultipleItem inlineFormMultipleItem)
  {
    // Do nothing.

  }



  /**
   * visit a {@link InlineFormSimpleItem}.
   * 
   * @param inlineFormSimpleItem
   *          the {@link InlineFormSimpleItem} to visit
   * @see com.genia.toolbox.web.gwt.form.client.visitor.FormVisitor#visitInlineFormSimpleItem(com.genia.toolbox.web.gwt.form.client.form.InlineFormSimpleItem)
   */
  public void visitInlineFormSimpleItem(InlineFormSimpleItem inlineFormSimpleItem)
  {
    // Do nothing.
  }



  /**
   * visit a {@link DynamicRadioButtonItem}.
   * 
   * @param item
   *          the {@link DynamicRadioButtonItem} to visit
   */
  public void visitDynamicRadioButtonItem(DynamicRadioButtonItem item)
  {
    visitSimpleItem(item);
  }



  /**
   * visit a {@link DynamicListBoxItem}.
   * 
   * @param item
   *          the {@link DynamicListBoxItem} to visit
   */
  public void visitDynamicListBoxItem(DynamicListBoxItem item)
  {
    visitSimpleItem(item);
  }

};